/**
 * Author jsan
 * SimpleTable 1.0.3 (2019-01-11T15:43:51+0800)
 * MIT License By https://gitee.com/JSanHo/simple.git
 */
(function($) {
    $.extend({
    	simTable : {
			create : function(object) {
				var table = {};
				var index = $.simTable.table_index++;
				var container = $(object.elem);	//获取容器
				container.addClass("container");
				var table_body_container = $.simTable.init_table_container(index, container);	//表格容器
				var header_table = $.simTable.init_header_table(index, container);	//表头
				var data_table = $.simTable.init_data_table(index, container);	//主体
				var foot_table = $.simTable.init_foot_table(index, container);	//脚部
				//加载表头
				var header = $.simTable.init_header(object.cols, container);
				//注入表头
				header_table.body.append(header.clone());
				data_table.body.append(header);
				//table容器
				var simple_container = $("<div class='simple-container'></div>");
				//放入表格容器
				table_body_container.header_table_container.append(header_table.body);
				table_body_container.data_table_container.append(data_table.body);
				//放入容器
				simple_container.width(container.width());
				simple_container.height(container.height());
				simple_container.append(table_body_container.data_table_container);
				simple_container.append(table_body_container.header_table_container);
				simple_container.scroll(function(e) {
					if(simple_container.scrollTop() != 0) {
						table_body_container.header_table_container.stop();
						table_body_container.header_table_container.css({"top": simple_container.scrollTop()});
					}else {
						table_body_container.header_table_container.stop();
						table_body_container.header_table_container.css({"top": "0"});
					}
				});
				$(object.elem).append(simple_container);
				//分页初始化
				if(object.page) {
					var page = $.simTable.init_table_page(index, container, object.sizeOption);
					$(object.elem).append(page);
					table.page = page;
					simple_container.height(container.height() - page.outerHeight(true));
				}
				table.index = index;
				table.header = header;
				table.header_table = header_table;
				table.data_table = data_table;
				table.foot_table = foot_table;
				table.table_body_container = table_body_container;
				table.simple_container = simple_container;
				table.property = object;
				$.simTable.tables[index] = table;
				//自适应大小设置
				if(object.resize) {
					$(window).resize(function() {
						simple_container.width($(object.elem).width());
						table.page.width($(object.elem).width());
						$.simTable.resize_table_width(table);
					});
				}
				//数据加载  动态数据加载为优先级
				if(object.url !== undefined) {
					$.simTable.load_url_data(1, table);
				}else if(object.data !== undefined) {	//静态数据加载项
					$.simTable.init_static_data(table);
				}
				$.simTable.resize_table_width(table);
				return table;
			},
			
			resize_table_width : function(table) {
				var object = table.property;
				var container_width = $(object.elem).width();
				var table_width = 0;
				var base_width = 0;
				for(var i in object.cols) {
					if(table.table_body_container.data_table_container.height() > $(object.elem).height() && Number(i)+1 === object.cols.length) {
						base_width = 20;
					}
					if(object.cols[i].width === undefined) {
						var th_width = container_width/object.cols.length-base_width;
						table.header_table.body.find("thead tr").eq(0).find("th").eq(i).css("width", th_width);
						table.data_table.body.find("thead tr").eq(0).find("th").eq(i).css("width", th_width);
						table_width += th_width;
					}else {
						var th_width = object.cols[i].width.indexOf("%") >= 0 ? container_width*Number(object.cols[i].width.replace("%", ""))*0.01-base_width : Number(object.cols[i].width.replace("px", "")-base_width);
						table.header_table.body.find("thead tr").eq(0).find("th").eq(i).css("width", th_width);
						table.data_table.body.find("thead tr").eq(0).find("th").eq(i).css("width", th_width);
						table_width += th_width;
					}
				}
				table.header_table.body.css("width", table_width);
				table.data_table.body.css("width", table_width);
				table.foot_table.body.css("width", table_width);
				table.table_body_container.header_table_container.css("width", table_width);
				table.table_body_container.data_table_container.css("width", table_width);
				table.table_body_container.foot_table_container.css("width", table_width);
			},
			
			init_table_page : function(index, container, size_option) {
				var page = $("<div id='simple_page_container_"+index+"' class='simple-page-container'></div>");
				var first_page = $("<span class='simple-table-page-btn first-page' table-index='"+index+"'><<</span>");
				var back_page = $("<span class='simple-table-page-btn back-page' table-index='"+index+"'><</span>");
				var page_number_container = $("<div class='simple-table-page_number-container' id='simple_table_page_number_container_"+index+"'></div>");
				var one = $("<span class='simple-table-page-btn page-active' table-index='"+index+"' data-param='1'>1</span>");
				page_number_container.append(one);
				var next_page = $("<span class='simple-table-page-btn next-page' table-index='"+index+"'>></span>");
				var last_page = $("<span class='simple-table-page-btn last-page' table-index='"+index+"'>>></span>");
				var go_page = $("<span>第</span>&nbsp;<input type='text' class='simple-table-page-go' id='simple_table_page_go_"+index+"' />&nbsp;<span>页</span>&nbsp;");
				var go_btn = $("<span class='simple-table-page-btn' table-index='"+index+"'>跳转</span>");
				var page_size_selected = $("<select class='simple-table-page-selected-size' id='simple_table_page_selected_size_"+index+"'></select>");
				if(size_option === undefined) {
					size_option = [30, 50, 100];
				}
				for(var i in size_option) {
					page_size_selected.append("<option value='"+size_option[i]+"'>"+size_option[i]+"</option>");
				}
				first_page.unbind().on("click", function() {
					var table = $.simTable.tables[index];
					if(table.nowPage === 1) {
						return;
					}
					if(table.property.url !== undefined) {
						$.simTable.load_url_data(1, table);
					}else if(table.property.data !== undefined) {
						$.simTable.init_static_data(table);
					}
				});
				back_page.unbind().on("click", function() {
					var table = $.simTable.tables[index];
					if(table.nowPage === 1) {
						return;
					}
					if(table.property.url !== undefined) {
						$.simTable.load_url_data(table.nowPage-1, table);
					}else if(table.property.data !== undefined) {
						$.simTable.load_page_numbers(table.index, table.nowPage-1, Math.ceil(table.property.data.length/Number(table.page.find("#simple_table_page_selected_size_"+table.index).val())), table);
						$.simTable.load_data(table.nowPage-1, Number(table.page.find("#simple_table_page_selected_size_"+table.index).val()), table.property.data.length, table.property, table);
					}
				});
				next_page.unbind().on("click", function() {
					var table = $.simTable.tables[index];
					if(table.nowPage === Math.ceil(table.total/table.pageSize)) {
						return;
					}
					if(table.property.url !== undefined) {
						$.simTable.load_url_data(table.nowPage+1, table);
					}else if(table.property.data !== undefined) {
						$.simTable.load_page_numbers(table.index, table.nowPage+1, Math.ceil(table.property.data.length/Number(table.page.find("#simple_table_page_selected_size_"+table.index).val())), table);
						$.simTable.load_data(table.nowPage+1, Number(table.page.find("#simple_table_page_selected_size_"+table.index).val()), table.property.data.length, table.property, table);
					}
				});
				last_page.unbind().on("click", function() {
					var table = $.simTable.tables[index];
					if(table.nowPage === Math.ceil(table.total/table.pageSize)) {
						return;
					}
					if(table.property.url !== undefined) {
						$.simTable.load_url_data(Math.ceil(table.total/table.pageSize), table);
					}else if(table.property.data !== undefined) {
						$.simTable.load_page_numbers(table.index, Math.ceil(table.total/table.pageSize), Math.ceil(table.property.data.length/Number(table.page.find("#simple_table_page_selected_size_"+table.index).val())), table);
						$.simTable.load_data(Math.ceil(table.total/table.pageSize), Number(table.page.find("#simple_table_page_selected_size_"+table.index).val()), table.property.data.length, table.property, table);
					}
				});
				page_size_selected.unbind().on("change", function() {
					var table = $.simTable.tables[index];
					if(table.property.url !== undefined) {
						$.simTable.load_url_data(1, table);
					}else if(table.property.data !== undefined) {
						$.simTable.init_static_data(table);
					}
				});
				var total_title = $("<span class='simple-table-page-total' id='simple_table_page_total_"+index+"'>共0行</span>");
				go_btn.unbind().on("click", function() {
					var table = $.simTable.tables[index];
					var page_number = Number(table.page.find("#simple_table_page_go_"+index).val());
					table.page.find("#simple_table_page_go_"+index).val("");
					if(table.property.url !== undefined) {
						$.simTable.load_url_data(page_number, table);
					}else if(table.property.data !== undefined) {
						$.simTable.load_page_numbers(table.index, page_number, Math.ceil(table.property.data.length/Number(table.page.find("#simple_table_page_selected_size_"+table.index).val())), table);
						$.simTable.load_data(page_number, Number(table.page.find("#simple_table_page_selected_size_"+table.index).val()), table.property.data.length, table.property, table);
					}
				});
				page.width(container.width());
				page.append(first_page);
				page.append(back_page);
				page.append(page_number_container);
				page.append(next_page);
				page.append(last_page);
				page.append(go_page);
				page.append(go_btn);
				page.append("<span>每页<span>&nbsp;");
				page.append(page_size_selected);
				page.append(total_title);
				return page;
			},
			
			/**
			 * 初始化表格容器
			 * @param {Object} index
			 */
			init_table_container : function(index, container) {
				var header = $("<div id='simple_header_table_container_"+index+"' class='simple-header-table-container'></div>");
				var data = $("<div id='simple_data_table_container_"+index+"' class='simple-data-table-container'></div>");
				var foot = $("<div id='simple_foot_table_container_"+index+"' class='simple-foot-table-container'></div>");
				return {
					header_table_container : header,
					data_table_container : data,
					foot_table_container : foot
				};
			},
			
			/**
			 * 初始化表头显示栏
			 * @param {Object} index 表索引
			 */
			init_header_table : function(index, container) {
				var obj = {
					id : "simple_header_table_"+index
				};
				var body = $("<table id='"+obj.id+"' class='simple-header-table'></table>");
				obj.body = body;
				return obj;
			},
			
			/**
			 * 初始化表主体显示栏
			 * @param {Object} index 表索引
			 */
			init_data_table : function(index, container) {
				var obj = {
					id : "simple_data_table_"+index
				};
				var body = $("<table id='"+obj.id+"' class='simple-data-table'></table>");
				obj.body = body;
				return obj;
			},
			
			/**
			 * 初始化表脚显示栏
			 * @param {Object} index 表索引
			 */
			init_foot_table : function(index, container) {
				var obj = {
					id : "simple_foot_table_"+index
				};
				var body = $("<table id='"+obj.id+"' class='simple-foot-table'></table>");
				obj.body = body;
				return obj;
			},
			
			/**
			 * 初始化表头
			 */
			init_header : function(cols, container) {
				var header = $("<thead></thead>");
				var tr = $("<tr></tr>");
				for(index in cols) {
					var th = $("<th></th>");
					th.attr("field", cols[index].field);
					th.html(cols[index].name);
					tr.append(th);
				}
				header.append(tr);
				return header;
			},
			
			init_static_data : function(table) {
				var index = table.index;
				var object = table.property;
				var sizeOption = object.data.length;	//获取每页最大行数
				var total = object.data.length;	//获取总行数
				var total_page = 1;	//总页数
				var now_page = 1; //当前页码
				if(table.page !== undefined) {
					sizeOption = Number(table.page.find("#simple_table_page_selected_size_"+index).val());
					total_page = Math.ceil(total/sizeOption);
					table.page.find("#simple_table_page_total_"+index).html("共"+total+"行");
					$.simTable.load_page_numbers(index, now_page, total_page, table);
				}
				table.total = total;
				table.pageSize = sizeOption;
				//加载数据
				$.simTable.load_data(now_page, sizeOption, total, object, table);
			},
			
			load_page_numbers : function(index, now_page, total_page, table) {
				table.page.find("#simple_table_page_number_container_"+index).html("");
				if(now_page === 1) {
					for(var i = 1, end = total_page <= 3 ? total_page : 3; i <= end; ++i) {
						var page = $("<span class='simple-table-page-btn"+(i === now_page ? " page-active" : "")+"' table-index='"+index+"' data-param='"+i+"'>"+i+"</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(page);
						if(i !== now_page) {
							$.simTable.go_to_page(page);
						}
					}
					if(total_page === 4) {
						var page = $("<span class='simple-table-page-btn"+(i === now_page ? " page-active" : "")+"' table-index='"+index+"' data-param='4'>4</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(page);
						$.simTable.go_to_page(page);
					}else if(total_page > 4) {
						table.page.find("#simple_table_page_number_container_"+index).append("<span>...</span>");
						var page = $("<span class='simple-table-page-btn"+(i === now_page ? " page-active" : "")+"' table-index='"+index+"' data-param='"+total_page+"'>"+total_page+"</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(page);
						$.simTable.go_to_page(page);
					}
				}else {
					if(now_page === total_page) {
						var first_page = $("<span class='simple-table-page-btn' table-index='"+index+"' data-param='1'>1</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(first_page);
						$.simTable.go_to_page(first_page);
						if(now_page - 3 > 1) {
							table.page.find("#simple_table_page_number_container_"+index).append("<span>...</span>");
						}
						var cross_page = $("<span class='simple-table-page-btn' table-index='"+index+"' data-param='"+(now_page - 2)+"'>"+(now_page - 2)+"</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(cross_page);
						$.simTable.go_to_page(cross_page);
					}else if(now_page + 1 === total_page) {
						var first_page = $("<span class='simple-table-page-btn' table-index='"+index+"' data-param='1'>1</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(first_page);
						$.simTable.go_to_page(first_page);
						if(now_page - 3 > 1) {
							table.page.find("#simple_table_page_number_container_"+index).append("<span>...</span>");
						}
					}else if(now_page >= 3) {
						var first_page = $("<span class='simple-table-page-btn' table-index='"+index+"' data-param='1'>1</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(first_page);
						$.simTable.go_to_page(first_page);
						if(now_page > 3) {
							table.page.find("#simple_table_page_number_container_"+index).append("<span>...</span>");
						}
					}
					var back_page = $("<span class='simple-table-page-btn' table-index='"+index+"' data-param='"+(now_page - 1)+"'>"+(now_page - 1)+"</span>");
					table.page.find("#simple_table_page_number_container_"+index).append(back_page);
					$.simTable.go_to_page(back_page);
					var page = $("<span class='simple-table-page-btn page-active' table-index='"+index+"' data-param='"+now_page+"'>"+now_page+"</span>");
					table.page.find("#simple_table_page_number_container_"+index).append(page);
					$.simTable.go_to_page(page);
					if(now_page + 1 <= total_page) {
						var next_page = $("<span class='simple-table-page-btn' table-index='"+index+"' data-param='"+(now_page + 1)+"'>"+(now_page + 1)+"</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(next_page);
						$.simTable.go_to_page(next_page);
					}
					if(now_page + 2 === total_page) {
						var page = $("<span class='simple-table-page-btn"+(i === now_page ? " page-active" : "")+"' table-index='"+index+"' data-param='"+total_page+"'>"+total_page+"</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(page);
						$.simTable.go_to_page(page);
					}else if(now_page + 2 < total_page) {
						table.page.find("#simple_table_page_number_container_"+index).append("<span>...</span>");
						var page = $("<span class='simple-table-page-btn"+(i === now_page ? " page-active" : "")+"' table-index='"+index+"' data-param='"+total_page+"'>"+total_page+"</span>");
						table.page.find("#simple_table_page_number_container_"+index).append(page);
						$.simTable.go_to_page(page);
					}
				}
				table.nowPage = now_page;
			},
			
			load_data : function(now_page, sizeOption, total, object, table) {
				var tbody = $("<tbody></tbody>");
				for(var i = (now_page - 1)*sizeOption, end = total < now_page*sizeOption ? total : now_page*sizeOption ; i < end; ++i) {
					var tr = $("<tr data-param='"+i+"'></tr>");
					for(var y in object.cols) {
						var td = $("<td></td>");
						var value = "";
						if(object.cols[y].hasOwnProperty("render")) {
							value = object.cols[y]["render"]((!object.data[i].hasOwnProperty(object.cols[y]["field"]) ? "" : object.data[i][object.cols[y]["field"]]), object.data[i]);
						}else {
							value = !object.data[i].hasOwnProperty(object.cols[y]["field"]) ? "" : object.data[i][object.cols[y]["field"]];
						}
						td.html(value);
						if(object.cols[y].hasOwnProperty("align")) {
							td.css("text-align", object.cols[y]["align"]);
						}
						tr.append(td);
					}
					tbody.append(tr);
				}
				table.data_table.body.find("tbody").remove();
				table.data_table.body.append(tbody);
				$.simTable.resize_table_width(table);
			},
			
			go_to_page : function(page) {
				page.unbind().on("click", function() {
					var table = $.simTable.tables[$(this).attr("table-index")];
					var next_page = $(this).attr("data-param");
					if(table.property.url !== undefined) {
						$.simTable.load_url_data(Number(next_page), table);
					}else if(table.property.data !== undefined) {
						$.simTable.load_page_numbers(table.index, Number(next_page), Math.ceil(table.property.data.length/Number(table.page.find("#simple_table_page_selected_size_"+table.index).val())), table);
						$.simTable.load_data(Number(next_page), Number(table.page.find("#simple_table_page_selected_size_"+table.index).val()), table.property.data.length, table.property, table);
					}
				});
			},
			
			load_url_data : function(page, table) {
				if(table.property.data === undefined) {
					table.property.data = {};
				}
				table.property.params.pageSize = table.page.find("#simple_table_page_selected_size_"+table.index).val();
				table.property.params.page = page;
				$.post(table.property.url, table.property.params, function(result) {
					table.total = result.total;
					table.pageSize = table.property.params.pageSize;
					table.property.data = result.rows;
					$.simTable.load_data(1, table.property.data.length, table.property.data.length, table.property, table);
					if(table.page !== undefined) {
						table.page.find("#simple_table_page_total_"+table.index).html("共"+table.total+"行");
						$.simTable.load_page_numbers(table.index, table.property.params.page, Math.ceil(table.total/table.property.params.pageSize), table);
					}
				});
			},
			
			table_index : 1,	//索引，初始值1
			
			tables : {}	//table属性表
		}
    });
})(jQuery);